#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<int> b, c;
    int cnt1 = 0, cnt2 = 0;
    for(int i = 0; i < n; i++) {
        if(a[i] == 1) {
            if(i && a[i - 1] == 2) {
                b.push_back(2);
                c.push_back(cnt2);
                cnt2 = 0;
            }
            cnt1++;
        }else{
            if(i && a[i - 1] == 1) {
                b.push_back(1);
                c.push_back(cnt1);
                cnt1 = 0;
            }
            cnt2++;
        }
    }
    if(a[n - 1] == 1) {
        b.push_back(1);
        c.push_back(cnt1);
    }else{
        b.push_back(2);
        c.push_back(cnt2);
    }
    // for(int i = 0; i < b.size(); i++) {
    //     cout << b[i] << ' ' << c[i] << endl;
    // }
    auto func = [&]()->int {
        int res = 0, t = 0;
        for(int i = 0; i < b.size(); i++) {
            if(i && b[i] < b[i  - 1]) {
                res = max(res, t);
                t = c[i];
            }
            else t += c[i];
        }
        res = max(res, t);
        return res;
    };
    int ans = 0;
    for(int i = 0; i < b.size() - 1; i++) {
        if(b[i] == 2 && b[i + 1] == 1){
            swap(b[i], b[i + 1]);
            ans = max(ans, func());
            swap(b[i], b[i + 1]);
        }else if(b[i] == 1 && b[i + 1] == 2) {
            swap(b[i], b[i + 1]);
            ans = max(ans, func());
            swap(b[i], b[i + 1]);
        }
    }
    cout << ans << endl;
}

int main()
{
    int t = 1;
    while (t--)
        solve();
    return 0;
}